﻿namespace Hidistro.UI.Web.Admin.sales
{
    using Hidistro.ControlPanel.Sales;
    using Hidistro.Entities;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.UI.ControlPanel.utility;
    using System;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Xml;

    public class Print : AdminPage
    {
        protected HtmlGenericControl divContent;
        protected string height = "";
        protected string mailNo = "";
        protected string orderIds = "";
        protected string templateName = "";
        protected string UpdateOrderIds = string.Empty;
        protected string width = "";

        private decimal CalculateOrderTotal(DataRow order, DataSet ds)
        {
            decimal result = 0M;
            decimal num2 = 0M;
            decimal num3 = 0M;
            decimal num4 = 0M;
            bool flag = false;
            decimal.TryParse(order["AdjustedFreight"].ToString(), out result);
            decimal.TryParse(order["PayCharge"].ToString(), out num2);
            string str = order["CouponCode"].ToString();
            decimal.TryParse(order["CouponValue"].ToString(), out num3);
            decimal.TryParse(order["AdjustedDiscount"].ToString(), out num4);
            bool.TryParse(order["OptionPrice"].ToString(), out flag);
            DataRow[] orderGift = ds.Tables[2].Select("OrderId='" + order["orderId"] + "'");
            DataRow[] orderLine = ds.Tables[1].Select("OrderId='" + order["orderId"] + "'");
            decimal num5 = this.GetAmount(orderGift, orderLine, order) + result;
            num5 += num2;
            if (!string.IsNullOrEmpty(str))
            {
                num5 -= num3;
            }
            return (num5 + num4);
        }

        public decimal GetAmount(DataRow[] orderGift, DataRow[] orderLine, DataRow order)
        {
            return (this.GetGoodDiscountAmount(order, orderLine) + this.GetGiftAmount(orderGift));
        }

        public decimal GetGiftAmount(DataRow[] rows)
        {
            decimal num = 0M;
            foreach (DataRow row in rows)
            {
                num += decimal.Parse(row["CostPrice"].ToString());
            }
            return num;
        }

        public decimal GetGoodDiscountAmount(DataRow order, DataRow[] orderLine)
        {
            decimal result = 0M;
            decimal.TryParse(order["DiscountAmount"].ToString(), out result);
            decimal goodsAmount = this.GetGoodsAmount(orderLine);
            order["ReducedPromotionName"].ToString();
            if (order["ReducedPromotionAmount"] != DBNull.Value)
            {
                goodsAmount = Convert.ToDecimal(order["ReducedPromotionAmount"]);
            }
            return goodsAmount;
        }

        public decimal GetGoodsAmount(DataRow[] rows)
        {
            decimal num = 0M;
            foreach (DataRow row in rows)
            {
                num += decimal.Parse(row["ItemAdjustedPrice"].ToString()) * int.Parse(row["Quantity"].ToString());
            }
            return num;
        }

        private DataSet GetPrintData(string orderIds)
        {
            orderIds = "'" + orderIds.Replace(",", "','") + "'";
            return OrderHelper.GetOrdersAndLines(orderIds);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.mailNo = base.Request["mailNo"];
                int shipperId = int.Parse(base.Request["shipperId"]);
                this.orderIds = base.Request["orderIds"].Trim(new char[]
		{
			','
		});
                string text = HttpContext.Current.Request.MapPath(string.Format("../../storage/master/flex/{0}", base.Request["template"]));
                if (File.Exists(text))
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(text);
                    XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("//printer");
                    this.templateName = xmlNode.SelectSingleNode("kind").InnerText;
                    string innerText = xmlNode.SelectSingleNode("pic").InnerText;
                    string innerText2 = xmlNode.SelectSingleNode("size").InnerText;
                    this.width = innerText2.Split(new char[]
			{
				':'
			})[0];
                    this.height = innerText2.Split(new char[]
			{
				':'
			})[1];
                    DataSet printData = this.GetPrintData(this.orderIds);
                    int num = 0;
                    foreach (DataRow dataRow in printData.Tables[0].Rows)
                    {
                        this.UpdateOrderIds = this.UpdateOrderIds + dataRow["orderid"] + ",";
                        HtmlGenericControl htmlGenericControl = new HtmlGenericControl("div");
                        if (!string.IsNullOrEmpty(innerText) && innerText != "noimage")
                        {
                            using (Image image = Image.FromFile(HttpContext.Current.Request.MapPath(string.Format("../../storage/master/flex/{0}", innerText))))
                            {
                                htmlGenericControl.Attributes["style"] = string.Format("background-image: url(../../storage/master/flex/{0}); width: {1}px; height: {2}px;text-align: center; position: relative;", innerText, image.Width, image.Height);
                            }
                        }
                        DataTable dataTable = printData.Tables[1];
                        ShippersInfo shipper = SalesHelper.GetShipper(shipperId);
                        string[] array = dataRow["shippingRegion"].ToString().Split(new char[]
				{
					'，'
				});
                        foreach (XmlNode xmlNode2 in xmlNode.SelectNodes("item"))
                        {
                            string text2 = xmlNode2.SelectSingleNode("name").InnerText;
                            StringBuilder stringBuilder = new StringBuilder(text2);
                            stringBuilder.Replace("收货人-姓名", dataRow["ShipTo"].ToString());
                            stringBuilder.Replace("收货人-电话", dataRow["TelPhone"].ToString());
                            stringBuilder.Replace("收货人-手机", dataRow["CellPhone"].ToString());
                            stringBuilder.Replace("收货人-邮编", dataRow["ZipCode"].ToString());
                            stringBuilder.Replace("收货人-地址", dataRow["Address"].ToString());
                            string newValue = string.Empty;
                            if (array.Length > 0)
                            {
                                newValue = array[0];
                            }
                            stringBuilder.Replace("收货人-地区1级", newValue);
                            newValue = string.Empty;
                            if (array.Length > 1)
                            {
                                newValue = array[1];
                            }
                            stringBuilder.Replace("收货人-地区2级", newValue);
                            newValue = string.Empty;
                            if (array.Length > 2)
                            {
                                newValue = array[2];
                            }
                            stringBuilder.Replace("收货人-地区3级", newValue);
                            string[] array2 = new string[]
					{
						"",
						"",
						""
					};
                            if (shipper != null)
                            {
                                array2 = RegionHelper.GetFullRegion(shipper.RegionId, "-").Split(new char[]
						{
							'-'
						});
                            }
                            stringBuilder.Replace("发货人-姓名", (shipper != null) ? shipper.ShipperName : "");
                            stringBuilder.Replace("发货人-手机", (shipper != null) ? shipper.CellPhone : "");
                            stringBuilder.Replace("发货人-电话", (shipper != null) ? shipper.TelPhone : "");
                            stringBuilder.Replace("发货人-地址", (shipper != null) ? shipper.Address : "");
                            stringBuilder.Replace("发货人-邮编", (shipper != null) ? shipper.Zipcode : "");
                            string newValue2 = string.Empty;
                            if (array2.Length > 0)
                            {
                                newValue2 = array2[0];
                            }
                            stringBuilder.Replace("发货人-地区1级", newValue2);
                            newValue2 = string.Empty;
                            if (array2.Length > 1)
                            {
                                newValue2 = array2[1];
                            }
                            stringBuilder.Replace("发货人-地区2级", newValue2);
                            newValue2 = string.Empty;
                            if (array2.Length > 2)
                            {
                                newValue2 = array2[2];
                            }
                            decimal num2 = 0m;
                            decimal.TryParse(dataRow["Weight"].ToString(), out num2);
                            stringBuilder.Replace("发货人-地区3级", newValue2);
                            stringBuilder.Replace("送货-上门时间", dataRow["ShipToDate"].ToString());
                            stringBuilder.Replace("订单-订单号", "订单号：" + dataRow["OrderId"].ToString());
                            stringBuilder.Replace("订单-总金额", decimal.Parse(dataRow["OrderTotal"].ToString()).ToString("F0"));
                            stringBuilder.Replace("订单-物品总重量", num2.ToString("F0"));
                            stringBuilder.Replace("订单-备注", dataRow["Remark"].ToString());
                            DataRow[] array3 = dataTable.Select(" OrderId='" + dataRow["OrderId"] + "'");
                            string text3 = string.Empty;
                            if (array3.Length > 0)
                            {
                                DataRow[] array4 = array3;
                                for (int i = 0; i < array4.Length; i++)
                                {
                                    DataRow dataRow2 = array4[i];
                                    text3 = string.Concat(new object[]
							{
								text3,
								"规格 ",
								dataRow2["SKUContent"],
								" ×",
								dataRow2["ShipmentQuantity"],
								"\n货号 :",
								dataRow2["SKU"]
							});
                                }
                                text3 = text3.Replace("；", "");
                            }
                            stringBuilder.Replace("订单-详情", text3);
                            stringBuilder.Replace("订单-送货时间", "");
                            stringBuilder.Replace("网店名称", HiContext.Current.SiteSettings.SiteName);
                            stringBuilder.Replace("自定义内容", "");
                            text2 = stringBuilder.ToString();
                            string innerText3 = xmlNode2.SelectSingleNode("font").InnerText;
                            string arg_700_0 = xmlNode2.SelectSingleNode("fontsize").InnerText;
                            string innerText4 = xmlNode2.SelectSingleNode("position").InnerText;
                            string innerText5 = xmlNode2.SelectSingleNode("align").InnerText;
                            string str = innerText4.Split(new char[]
					{
						':'
					})[0];
                            string str2 = innerText4.Split(new char[]
					{
						':'
					})[1];
                            string str3 = innerText4.Split(new char[]
					{
						':'
					})[2];
                            string str4 = innerText4.Split(new char[]
					{
						':'
					})[3];
                            HtmlGenericControl htmlGenericControl2 = new HtmlGenericControl("div");
                            htmlGenericControl2.Visible = true;
                            htmlGenericControl2.InnerText = text2.Split(new char[]
					{
						'_'
					})[0];
                            htmlGenericControl2.Style["font-family"] = innerText3;
                            htmlGenericControl2.Style["font-size"] = "16px";
                            htmlGenericControl2.Style["width"] = str + "px";
                            htmlGenericControl2.Style["height"] = str2 + "px";
                            htmlGenericControl2.Style["text-align"] = innerText5;
                            htmlGenericControl2.Style["position"] = "absolute";
                            htmlGenericControl2.Style["left"] = str3 + "px";
                            htmlGenericControl2.Style["top"] = str4 + "px";
                            htmlGenericControl2.Style["padding"] = "0";
                            htmlGenericControl2.Style["margin-left"] = "0px";
                            htmlGenericControl2.Style["margin-top"] = "0px";
                            htmlGenericControl.Controls.Add(htmlGenericControl2);
                        }
                        this.divContent.Controls.Add(htmlGenericControl);
                        num++;
                        if (num < printData.Tables[0].Rows.Count)
                        {
                            HtmlGenericControl htmlGenericControl3 = new HtmlGenericControl("div");
                            htmlGenericControl3.Attributes["class"] = "PageNext";
                            this.divContent.Controls.Add(htmlGenericControl3);
                        }
                    }
                    this.UpdateOrderIds = this.UpdateOrderIds.TrimEnd(new char[]
			{
				','
			});
                }
            }
        }
    }
}

